package repository

import (
	"gorm.io/gorm/clause"
	"niu-video/app/model"
	"niu-video/pkg/db"
)

var CategoryRepository = newCategoryRepository()

func newCategoryRepository() *categoryRepository {
	return &categoryRepository{}
}

type categoryRepository struct {
	baseRepository
}

// All 获取所有用户数据
func (c *categoryRepository) All() (categories []model.Category) {
	db.DB.Order("orders").Find(&categories)
	return
}
func (c *categoryRepository) GetOne(id int64) (category model.Category) {
	db.DB.Where("category_id", id).First(&category)
	return
}

func (c *categoryRepository) GetVideoByCategory(page int, pageSize int, categoryId int64, order clause.OrderByColumn) (videos []model.Video) {
	//err = db.DB.Debug().Model(&model.Category{CategoryID: categoryId}).Order(order).Offset((page - 1) * pageSize).Limit(pageSize).Association("Videos").Find(&videos)
	db.DB.Where("category_id = ?", categoryId).
		Order(order).
		Offset((page-1)*pageSize).
		Limit(pageSize).
		Where("is_delete", false).
		Find(&videos)
	return videos
}
